import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
import legacy from '@vitejs/plugin-legacy'
// https://vite.dev/config/
export default defineConfig(( command
    , mode
    , isSsrBuild
    , isPreview
)=>{
  console.log(command,mode)
  return {
    plugins: [react(), legacy({
      targets: ['defaults', 'not IE 11', 'chrome 52']
    }),],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src')
      }
    },
    // server: {
    //   host: '0.0.0.0',
    //   port: '8000 ',
    //    proxy: {
    //    '/api': {
    //       target: `${mode ==='dev' ? 'https://api.591adb.com/release_platform_api_test':'https://api.591adb.com/release_platform_api'}`,
    //       changeOrigin: true,
    //       rewrite: (path) => path.replace(/^\/api/, ''),
    //      },
    //    },
    // },
    build: {
      chunkSizeWarningLimit: 3000,
      outDir: 'dist',
      assetsDir: 'assets', //指定静态资源存放路径
      sourcemap: false, //是否构建source map 文件
      emptyOutDir: true, // 是否清空目录
      // // 去除console 和 debugger
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      },
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return 'vendor'
            }
          },
          entryFileNames: 'js/[name].[hash].js',
          chunkFileNames: 'js/[name].[hash].js',
          assetFileNames: ({name}) => {
            if (/\.(gif|jpe?g|png|svg)$/.test(name ?? '')) {
              return 'img/[name].[hash][extname]';
            }
            if (/\.css$/.test(name ?? '')) {
              return 'css/[name].[hash][extname]';
            }
            if (/\.(woff2?|eot|ttf|otf)$/.test(name ?? '')) {
              return 'font/[name].[hash][extname]';
            }
            return 'other/[name].[hash][extname]';
          }
        }
      }
    }
  }

})
